package net.wanho.ums.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.ums.constant.UserConstant;
import net.wanho.ums.entity.User;
import net.wanho.ums.exception.NameExistsException;
import net.wanho.ums.mapper.UserMapper;
import net.wanho.ums.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Author：汤小洋
 * Date：2023-09-22 9:36
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public void add(User user) {
        /**
         * 判断用户是否存在
         */
        User u = userMapper.selectByName(user.getName());
        if (u!=null){
            throw new NameExistsException("用户名已存在！");
        }

        user.setStatus(UserConstant.USER_STATUS_ENABLE);

        userMapper.insert(user);
    }

    @Override
    public PageInfo<User> findByPage(int pageNum, int pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<User>(userMapper.selectByCondition(user));
    }

    @Override
    public void removeById(int id) {
        userMapper.deleteById(id);
    }
}
